﻿using AIStudio.Wpf.Demo01.Models;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace AIStudio.Wpf.Demo01.Models
{
    public class SharpBase : BindableBase
    {
        private double _left;
        public double Left
        {
            get { return _left; }
            set { SetProperty(ref _left, value); }
        }

        private double _top;
        public double Top
        {
            get { return _top; }
            set { SetProperty(ref _top, value); }
        }

        private double _width;
        public double Width
        {
            get
            {
                return _width;
            }
            set
            {
                SetProperty(ref _width, value);
            }
        }

        private double _height;
        public double Height
        {
            get
            {
                return _height;
            }
            set
            {
                SetProperty(ref _height, value);
            }
        }

        protected ObservableCollection<FullyPort> _ports = new ObservableCollection<FullyPort>();
        public ObservableCollection<FullyPort> Ports
        {
            get
            {
                return _ports;
            }
        }

        public FullyPort TopPort
        {
            get
            {
                return Ports?.FirstOrDefault(p => p.Orientation == PortOrientation.Top);
            }
        }

        public FullyPort BottomPort
        {
            get
            {
                return Ports?.FirstOrDefault(p => p.Orientation == PortOrientation.Bottom);
            }
        }

        public FullyPort LeftPort
        {
            get
            {
                return Ports?.FirstOrDefault(p => p.Orientation == PortOrientation.Left);
            }
        }

        public FullyPort RightPort
        {
            get
            {
                return Ports?.FirstOrDefault(p => p.Orientation == PortOrientation.Right);
            }
        }

        public SharpBase()
        {
            Ports.Add(new FullyPort() { Orientation = PortOrientation.Left });
            Ports.Add(new FullyPort() { Orientation = PortOrientation.Top });
            Ports.Add(new FullyPort() { Orientation = PortOrientation.Right });
            Ports.Add(new FullyPort() { Orientation = PortOrientation.Bottom });

            this.PropertyChanged += SharpBase_PropertyChanged;
        }

        private double portWidth = 8;
        private double portHeight = 8;
        private void SharpBase_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(Left) || e.PropertyName == nameof(Top) || e.PropertyName == nameof(Width) || e.PropertyName == nameof(Height))
            {
                LeftPort.Position = new Point(Left , Top + Height / 2 );
                TopPort.Position =  new Point(Left + Width / 2 , Top);               
                RightPort.Position = new Point(Left + Width , Top + Height / 2 );
                BottomPort.Position = new Point(Left + Width / 2 , Top + Height );
            }
        }
    }
}
